feat(cli): support stdin/stdout I/O using the single hyphen convention#183
feat(cli): support stdin/stdout I/O using the single hyphen convention#183pointlander merged 1 commit intopointlander:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for the standard Unix convention of using a single hyphen (-) to represent stdin/stdout in the PEG CLI tool. The changes enable reading from stdin when no arguments are provided or when - is specified as input, and writing to stdout when -output "-" is specified.
- Refactored command-line argument processing to support stdin/stdout via hyphen convention
- Restructured main function into smaller, more focused functions (
getIOandparse) - Updated flag help text to clarify stdout support
Comments suppressed due to low confidence (1)
main.go:121
- [nitpick] The parameter name
compileshadows the actual purpose of this function. Consider renaming it tocompileFuncorcompileFnto better reflect that it's a function parameter.
return compile(p, out)
| } | ||
| fmt.Fprintln(os.Stderr, "warning:", err) |
There was a problem hiding this comment.
The error handling logic is incorrect. When *strict is true, the code calls log.Fatal(err), but when false, it prints a warning. However, this happens outside the conditional check for err != nil. The log.Fatal(err) should only be called when there's actually an error.
There was a problem hiding this comment.
Both logging statements are inside the conditional check for err != nil. This looks like a Copilot mistake.
| if in != nil && in != os.Stdin { | ||
| in.Close() | ||
| } |
There was a problem hiding this comment.
The resource cleanup logic is duplicated between lines 83-85 and 100-102. Consider extracting this into a helper function or using defer statements to avoid code duplication.
| if in != nil && in != os.Stdin { | |
| in.Close() | |
| } | |
| closeResource(in, os.Stdin) |
There was a problem hiding this comment.
Disagree with this suggestion: the reader would have to jump to another function containing only 2 lines of code and then jump back to resume reading.
So, adding closeResource is tedious for the reader and serves no functional benefit.
|
Thanks! |
This change refactors much of
main.gowhere command-line flags are processed.The behavioral differences are described under their respective branches:
main(currentpegcommand)cli/stdio-hyphen(this PR)---output "-"--outputunspecifiedinput + ".go"mainif input is not stdin; otherwise, write parser to stdoutThe usage string for flag
--outputwas also changed from:to:
There are no changes in all other circumstances.